package com.yanqu.road.server.gameplayer.module.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eReduceItemStatus;
import com.yanqu.road.entity.platform.PlatformReduceItemOrder;
import com.yanqu.road.logic.bussiness.platform.ReduceItemBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.agent.inter.AgentPlayer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 扣除道具模块
 */
public class ReduceItemModule extends GeneralModule {

    public ReduceItemModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public void afterLogin() {

    }

    /**
     * 扣道具
     */
    public synchronized String doReduceItem(PlatformReduceItemOrder reduceItemOrder) {
        JSONObject resultObj = new JSONObject();
        log.info("订单正在扣除{}", reduceItemOrder.getOrderNo());

        Property reduceItem = new Property(reduceItemOrder.getItemId(), BigInteger.valueOf(reduceItemOrder.getItemCount()));

        if (reduceItemOrder.getOrderType() == 1) {
            // 扣除
            // 判断下是否扣除
            if (reduceItemOrder.getStatus() == eReduceItemStatus.AlreadyReduce.getValue()) {
                resultObj.put("state", 2);
                resultObj.put("msg", "订单已经执行扣除" + reduceItemOrder.getOrderNo());
                return resultObj.toJSONString();
            }
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(reduceItem)) {
                reduceItemOrder.setStatus(eReduceItemStatus.Default.getValue());
            } else {
                // 成功扣除
                reduceItemOrder.setStatus(eReduceItemStatus.AlreadyReduce.getValue());
            }
        } else if (reduceItemOrder.getOrderType() == 2) {
            // 增加
            // 判断下是否已扣除
            if (reduceItemOrder.getStatus() != eReduceItemStatus.AlreadyReduce.getValue()) {
                resultObj.put("state", 0);
                resultObj.put("msg", "订单未执行扣除" + reduceItemOrder.getOrderNo());
                return resultObj.toJSONString();
            }
            reduceItemOrder.setStatus(eReduceItemStatus.Default.getValue());
        }
        if (ReduceItemBussiness.updatePlatformReduceItemOrderStatus(reduceItemOrder)) {
            // 扣之前数量
            long beforeNum = player.getModule(BagModule.class).getUserBagItemCount(reduceItemOrder.getItemId());
            if (reduceItemOrder.getOrderType() == 1) {
                // 扣除
                if (!player.getModule(CurrencyModule.class).removeCurrency(reduceItem, eLogMoneyType.GM, eLogMoneyType.GmDeductGoods,
                        reduceItemOrder.getOrderNo() + "，扣除道具:" + reduceItemOrder.getItemId() + ",数量：" + reduceItemOrder.getItemCount())) {//不够扣除，直接封禁
                    resultObj.put("state", 0);
                    resultObj.put("msg", "道具不够扣除" + reduceItemOrder.getOrderNo());
                    return resultObj.toJSONString();
                }
                // 成功扣除，发个邮件
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.MAIL_TOKENS_DEDUCT_TITLE, player.getLanguage());
                String mailContent = MultipleLanguageMgr.getContent(MailManager.MAIL_TOKENS_DEDUCT_CONTENT, player.getLanguage());
                // 奖励
                MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), "", mailContent, mailTitle);
            } else if (reduceItemOrder.getOrderType() == 2) {
                // 增加道具
                player.getModule(CurrencyModule.class).addCurrency(reduceItem, eLogMoneyType.GM, eLogMoneyType.GmSend);
                // 成功补发，发个邮件
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.MAIL_TOKENS_REISSUE_TITLE, player.getLanguage());
                String mailContent = MultipleLanguageMgr.getContent(MailManager.MAIL_TOKENS_REISSUE_CONTENT, player.getLanguage());
                // 奖励
                MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), "", mailContent, mailTitle);
            }

            // 扣之后数量
            long afterNum = player.getModule(BagModule.class).getUserBagItemCount(reduceItemOrder.getItemId());
            resultObj.put("state", 1);
            resultObj.put("msg", "success");
            JSONObject dataObj = new JSONObject();
            dataObj.put("voucherNum", beforeNum);
            dataObj.put("voucherVariation", reduceItemOrder.getItemCount());
            dataObj.put("voucherRest", afterNum);
            resultObj.put("data", dataObj);
            return resultObj.toJSONString();
        }
        resultObj.put("state", 0);
        resultObj.put("msg", "订单扣除失败" + reduceItemOrder.getOrderNo());
        return resultObj.toJSONString();
    }
}
